package com.softer.chengxin.project.dao;

import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.person.domin.PersonVO;
import com.softer.chengxin.project.dao.provider.ProManagerDaoProvider;
import com.softer.chengxin.project.domin.CompanyHouseVO;
import com.softer.chengxin.project.domin.Project;
import com.softer.chengxin.project.domin.ProjectInfoVO;
import com.softer.chengxin.project.domin.ProprietorVO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author wangxinrui
 * @date 2018/10/12
 */
@Mapper
@Repository
public interface ProManagerDao {
    /**
     * 根据用户名查询diccode
     * @param loginName
     * @return
     */
    @Select("SELECT dic_code FROM roules r LEFT JOIN manager_user m on r.id = m.role_id WHERE m.login_name = #{name}")
    String getDicCodeByLoginName(@Param("name") String loginName);

    /**
     * 加载企业项目信息
     *
     * @param qyName      企业名
     * @param state       状态
     * @param area        该账号所管理的地区
     * @param page        页码
     * @param district    项目所在区县
     * @param street      项目所在街道
     * @param cxBeginDate 查询开始日期
     * @param cxEndDate   查询结束日期
     * @return 结果
     */
    @SelectProvider(type = ProManagerDaoProvider.class, method = "load")
    List<ProjectInfoVO> load(@Param("qyName") String qyName,
                             @Param("projectName") String projectName,
                             @Param("state") String state,
                             @Param("area") String area,
                             @Param("page") Page page,
                             @Param("district") String district,
                             @Param("street") String street,
                             @Param("cxBeginDate") String cxBeginDate,
                             @Param("cxEndDate") String cxEndDate);

    /**
     * 加载企业项目信息 条数
     *
     * @param qyName      企业名
     * @param state       状态
     * @param area        该账号所管理的地区
     * @param district    项目所在区县
     * @param street      项目所在街道
     * @param cxBeginDate 查询开始日期
     * @param cxEndDate   查询结束日期
     * @return 结果
     */
    @SelectProvider(type = ProManagerDaoProvider.class, method = "loadCount")
    int loadCount(@Param("qyName") String qyName,
                  @Param("projectName") String projectName,
                  @Param("state") String state,
                  @Param("area") String area,
                  @Param("district") String district,
                  @Param("street") String street,
                  @Param("cxBeginDate") String cxBeginDate,
                  @Param("cxEndDate") String cxEndDate);

    /**
     * 查询企业项目信息
     *
     * @param qyName  企业名
     * @param state   状态
     * @param area    区域
     * @param page    页码
     * @param proName 页码
     * @return 结果
     */
    @SelectProvider(type = ProManagerDaoProvider.class, method = "findByCondition")
    List<ProjectInfoVO> findByCondition(@Param("qyName") String qyName,
                                        @Param("state") Integer state,
                                        @Param("area") String area,
                                        @Param("page") Page page,
                                        @Param("proName") String proName);

    /**
     * 查询企业项目信息 条数
     *
     * @param qyName 企业名
     * @param state  状态
     * @param area   区域
     * @return 结果
     */
    @SelectProvider(type = ProManagerDaoProvider.class, method = "findByConditionCount")
    int findByConditionCount(@Param("qyName") String qyName,
                             @Param("state") Integer state,
                             @Param("area") String area,
                             @Param("proName") String proName);

    /**
     * 审批项目信息
     *
     * @param proId 项目id
     * @param state 状态
     * @return 结果
     */
    @Update("update company_project set state=#{state},gmt_modified=now() WHERE id=#{proId}")
    int sp(@Param("proId") Integer proId, @Param("state") Integer state);

    /**
     * 通过项目id查询企业id
     *
     * @param id 项目id
     * @return 企业id
     */
    @Select("select company_id from company_project where id=#{proId}")
    Integer findCorpId(@Param("proId") Integer id);

    /**
     * 通过项目id获取企业id
     *
     * @param proId 项目id
     * @return 企业id
     */
    @Select("select p.id as id,DATE_FORMAT(p.gmt_create, '%Y-%m-%d') as gmtCreate,DATE_FORMAT(p.gmt_modified, '%Y-%m-%d') as gmtModified,p.project_id as projectId,p.project_name as projectName,p.company_id as companyId,p.property_type AS propertyTypeCode,(SELECT GROUP_CONCAT(name) FROM dic_info WHERE FIND_IN_SET(CODE,p.property_type)) AS propertyType,p.province as province,p.city as city,p.district as district,p.street as street,p.community,p.project_address as projectAddress,p.project_develop as projectDevelop,p.with_elevator_area as withElevatorArea,p.public_facilities_area as publicFacilitiesArea,p.housing_quantity as housingQuantity,p.non_residential_quantity as nonResidentialQuantity,p.number_residents as numberResidents,p.elevators as elevators,p.committee_time as committeeTime,p.committee_director as committeeDirector,p.east_end as eastEnd,p.west_end as westEnd,p.south_end as southEnd,p.north_end as northEnd,p.project_area as projectArea,p.management_phone as managementPhone,p.building_land as buildingLand,p.green_area as greenArea,p.municipal as municipal,p.other_area as otherArea,p.all_area as allArea,p.get_type as getType,p.takeover_time as takeoverTime,p.state as state,p.is_old as isOld,p.is_indemnificatory as isIndemnificatory,p.finish_time as finishTime,p.is_intelligent as isIntelligent,p.contract_path as contractPath,p.remark as remark,p.air_scape as airScape,p.layout_plan as layoutPlan,p.service_contract as serviceContract " +
            "from company_project p where p.id=#{proId}")
    ProjectInfoVO getCompanyId(@Param("proId") int proId);

    /**
     * 获取该项目下的人员
     *
     * @param page  页码
     * @param name  人员姓名
     * @param depId 部门id
     * @param proId 项目id
     * @return 结果
     */
    @SelectProvider(type = ProManagerDaoProvider.class, method = "findProjectPerson")
    List<PersonVO> findProjectPerson(@Param("page") Page page,
                                     @Param("name") String name,
                                     @Param("depId") String depId,
                                     @Param("proId") Integer proId);

    /**
     * 获取该项目下的人员 条数
     *
     * @param name  人员姓名
     * @param depId 部门id
     * @param proId 项目id
     * @return 结果
     */
    @SelectProvider(type = ProManagerDaoProvider.class, method = "findProjectPersonCount")
    int findProjectPersonCount(@Param("name") String name,
                               @Param("depId") String depId,
                               @Param("proId") Integer proId);

    /**
     * 获取该管理账号下的企业下的项目的物业用房信息
     *
     * @param page    页码
     * @param address 物业用房详细地址
     * @param nature  物业用房性质
     * @param proId   项目id
     * @return 结果
     */
    @SelectProvider(type = ProManagerDaoProvider.class, method = "findProjectWYYF")
    List<CompanyHouseVO> findProjectWYYF(@Param("page") Page page,
                                         @Param("address") String address,
                                         @Param("nature") Integer nature,
                                         @Param("proId") Integer proId);

    /**
     * 获取该管理账号下的企业下的项目的物业用房信息 条数
     *
     * @param address 物业用房详细地址
     * @param nature  物业用房性质
     * @return 结果
     */
    @SelectProvider(type = ProManagerDaoProvider.class, method = "findProjectWYYFCount")
    int findProjectWYYFCount(@Param("address") String address,
                             @Param("nature") Integer nature,
                             @Param("proId") Integer proId);

    /**
     * 获取该账号管理区域下的项目下的委员会信息
     *
     * @param page       页码
     * @param name       人员姓名
     * @param sex        性别
     * @param proId      项目id
     * @param isDirector 是否主任
     * @return 结果
     */
    @SelectProvider(type = ProManagerDaoProvider.class, method = "findProjectCommittee")
    List<ProprietorVO> findProjectCommittee(@Param("page") Page page,
                                            @Param("name") String name,
                                            @Param("sex") Integer sex,
                                            @Param("proId") Integer proId,
                                            @Param("isDirector") Integer isDirector);

    /**
     * 获取该账号管理区域下的项目下的委员会信息 条数
     *
     * @param name       人员姓名
     * @param sex        性别
     * @param proId      项目id
     * @param isDirector 是否主任
     * @return 结果
     */
    @SelectProvider(type = ProManagerDaoProvider.class, method = "findProjectCommitteeCount")
    int findProjectCommitteeCount(@Param("name") String name,
                                  @Param("sex") Integer sex,
                                  @Param("proId") Integer proId,
                                  @Param("isDirector") Integer isDirector);

    /**
     * 查询登陆账号的管理区域
     *
     * @param id 账号id
     * @return 管理区域
     */
    @Select("SELECT area FROM manager_user WHERE id=#{id}")
    String getArea(@Param("id") Integer id);

    /**
     * 选中地图上的位置，并显示相应信息
     *
     * @param area 登陆账号的管理区域
     * @return 信息集合
     */
    @Select("select p.id,p.project_name as projectName,c.name as companyName,p.property_type AS propertyTypeCode,(SELECT GROUP_CONCAT(name) FROM dic_info WHERE FIND_IN_SET(CODE,p.property_type)) AS propertyType,p.all_area as allArea,r.`level` as dj,p.project_address as projectAddress " +
            " from company_project p left join company_base_info c on p.company_id=c.id left join score_composite r ON c.id = r.company_id  where (CASE p.community WHEN 0 THEN p.street in in(${area}) ELSE p.community in (${area}) end )")
    List<Project> getProInfoHome(@Param("area") String area);
}
